/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.home.tax;

import java.awt.Dialog;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.home.tax.dao.HoSoRepository;
import org.home.tax.model.DoanhNghiep;
import org.home.tax.model.HoSo;
import org.omg.PortableInterceptor.SYSTEM_EXCEPTION;
import sun.awt.FocusingTextField;

/**
 *
 * @author Home
 */
public class ThemHoSo_TrucTiep extends javax.swing.JFrame {

    /**
     * Creates new form NewDocForm
     */
    public ThemHoSo_TrucTiep() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        txtCanBoGiuNS = new javax.swing.JTextField();
        txtDiaChi = new javax.swing.JTextField();
        txtTenDoanhNghiep = new javax.swing.JTextField();
        txtMST = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        txtTruongDoan = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        txtDateCoQuanThue = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jLabel8 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("Ngày hiện tại:");

        jLabel2.setText("dd/mm/yyyy");

        jLabel3.setText("Mã Số Thuế:");

        jLabel5.setText("Doanh nghiệp:");

        jLabel7.setText("Địa Chỉ:");

        jLabel9.setText("Cán bộ giữ ngân sách:");

        txtCanBoGiuNS.setText("Nguyen Van Tai");

        txtDiaChi.setText("20 Nguyen Thi Nho, Q.10");

        txtTenDoanhNghiep.setText("Cong ty May mac 23/10");
        txtTenDoanhNghiep.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtTenDoanhNghiepActionPerformed(evt);
            }
        });

        txtMST.setText("MST00002");
        txtMST.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtMSTFocusLost(evt);
            }
        });

        jLabel4.setText("Trưởng Đoàn:");

        txtTruongDoan.setText("Tran Thanh Nam");

        jLabel6.setText("Ngày thực hiện tại cơ quan thuế");

        txtDateCoQuanThue.setText("30/03/2012");

        jButton1.setText("Save");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Clear");

        jButton3.setText("Cancel");

        jLabel8.setText("//kiểm tra ma so thuế đã tồn tại");

        jLabel10.setText("//auto fill nếu tồn tại mst");

        jLabel11.setText("//Nhap tai day hay ko");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel6)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txtDateCoQuanThue, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel11))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel9)
                                    .addComponent(jLabel4)
                                    .addComponent(jLabel3)
                                    .addComponent(jLabel5)
                                    .addComponent(jLabel7))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(2, 2, 2)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(txtDiaChi, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addGroup(layout.createSequentialGroup()
                                                .addComponent(txtTenDoanhNghiep, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(jLabel10))
                                            .addGroup(layout.createSequentialGroup()
                                                .addComponent(txtMST, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addGap(18, 18, 18)
                                                .addComponent(jLabel8))))
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                        .addComponent(txtTruongDoan, javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(txtCanBoGiuNS, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 107, Short.MAX_VALUE))))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(100, 100, 100)
                        .addComponent(jButton1)
                        .addGap(18, 18, 18)
                        .addComponent(jButton2)
                        .addGap(18, 18, 18)
                        .addComponent(jButton3)))
                .addContainerGap(32, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txtMST, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(txtTenDoanhNghiep, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel10))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(txtDiaChi, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(txtCanBoGiuNS, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(txtTruongDoan, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(txtDateCoQuanThue, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel11))
                .addGap(43, 43, 43)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2)
                    .addComponent(jButton3))
                .addContainerGap(48, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void txtMSTFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtMSTFocusLost
        // TODO add your handling code here:
        System.out.println("ok---------------");
        try{
        Conn.connect();
         Statement stat = Conn.conn.createStatement();
        String MST = txtMST.getText();
        ResultSet rs = stat.executeQuery("select * from DoanhNghiep where MaSoThue = '"+MST+"';");
        while (rs.next()) {
            String TenDN = rs.getString("TenDoanhNghiep");
            String DiaChi = rs.getString("DiaChi");
            txtTenDoanhNghiep.setText(TenDN);
            txtDiaChi.setText(DiaChi);
            txtTruongDoan.setFocusable(true);
            System.out.println("MST = " + rs.getString("Masothue"));
            System.out.println("Ten = " + rs.getString("TenDoanhNghiep"));
        }
        rs.close();
        Conn.disconnect();
        } catch (SQLException ex) {
            Logger.getLogger(Tax.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_txtMSTFocusLost

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        HoSo hoso = new HoSo();
        DoanhNghiep doanhnghiep = new DoanhNghiep();
        doanhnghiep.setMaSoThue(txtMST.getText());
        doanhnghiep.setTenDoanhNghiep(txtTenDoanhNghiep.getText());
        doanhnghiep.setDiaChi(txtDiaChi.getText());
        
        hoso.setMaSoThue(doanhnghiep);
        hoso.setCongChucGiuNS(txtCanBoGiuNS.getText());
        hoso.setTruongDoan(txtTruongDoan.getText());
        hoso.setDateTaoHoSo(new SimpleDateFormat("dd/MM/yyyy").format(new Date()));
        
        HoSoRepository hosoRepo = new HoSoRepository();
        hosoRepo.saveOrUpdate(hoso);
//        try{
//        Conn.connect();
//         PreparedStatement prep = Conn.conn.prepareStatement(
//                  "insert into HoSo(MaSoThue, CongChucGiuNS, TruongDoan,DateTaoHoSO) values (?,?,?,?);");
//
//             // prep.setInt(1, 2);
//              prep.setString(1, txtMST.getText());
//              prep.setString(2, txtCanBoGiuNS.getText());
//              prep.setString(3,txtTruongDoan.getText());
//              prep.setString(4, "23/03/2012");
//              //prep.setString(5, txtDateCoQuanThue.getText());
//              
//              prep.addBatch();
//              
//              Conn.conn.setAutoCommit(false);
//              prep.executeBatch();
//              Conn.conn.setAutoCommit(true);
//              
//              prep = Conn.conn.prepareStatement(
//                  "insert into DoanhNghiep(MaSoThue, TenDoanhNghiep,DiaChi) values (?,?,?);");
//              prep.setString(1, txtMST.getText());
//              prep.setString(2, txtTenDoanhNghiep.getText());
//              prep.setString(3,txtDiaChi.getText());
//              prep.addBatch();
//              Conn.conn.setAutoCommit(false);
//              prep.executeBatch();
//              Conn.conn.setAutoCommit(true);
//              
//        Conn.disconnect();
//        } catch (SQLException ex) {
//            Logger.getLogger(Tax.class.getName()).log(Level.SEVERE, null, ex);
//        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void txtTenDoanhNghiepActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtTenDoanhNghiepActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtTenDoanhNghiepActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ThemHoSo_TrucTiep.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ThemHoSo_TrucTiep.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ThemHoSo_TrucTiep.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ThemHoSo_TrucTiep.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new ThemHoSo_TrucTiep().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JTextField txtCanBoGiuNS;
    private javax.swing.JTextField txtDateCoQuanThue;
    private javax.swing.JTextField txtDiaChi;
    private javax.swing.JTextField txtMST;
    private javax.swing.JTextField txtTenDoanhNghiep;
    private javax.swing.JTextField txtTruongDoan;
    // End of variables declaration//GEN-END:variables
}
